package com.zhongwang.mms.module.material.service;

import com.zhongwang.mms.base.BaseService;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.MChangeOrder;
import com.zhongwang.mms.module.material.dao.MatRetManageDao;
import com.zhongwang.mms.module.material.model.ChangeOrderRetMatModel;
import com.zhongwang.mms.module.material.model.MChangeOrderModel;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 徐一贺
 * @className MatRetManageService
 * @description 还料管理业务逻辑层
 * @date 2020/11/28 9:24
 * @versron ver 1.0
 */
@Log4j2
@Service
public class MatRetManageService extends BaseService {
    @Autowired
    private MatRetManageDao dao;


    /**
     * findMChangeOrderByQuery 还料列表
     *
     * @param changeOrderModel 检索参数
     * @return java.util.List<com.zhongwang.mms.gen.entity.MChangeOrder> 还料列表数据
     * @author 徐一贺
     * @date 2020/11/28 10:02
     */
    public List<ChangeOrderRetMatModel> findMChangeOrderByQuery(MChangeOrderModel changeOrderModel) {
        try {
            return dao.findMChangeOrderByQuery(changeOrderModel);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return dao.findMChangeOrderByQuery(changeOrderModel);
    }

    /**
     * confirm 确认还料
     *
     * @param param 参数
     * @return com.zhongwang.mms.common.JsonResult
     * @throws
     * @author 徐一贺
     * @date 2020/11/28 12:41
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult confirm(ChangeOrderRetMatModel param) {
        JsonResult result = new JsonResult();
        try {
            dao.updateMChangeOrderByQuery(param);
            logService.saveLog("还料提醒","确认还料提醒！");
            result.setType(JsonResult.ResultType.SUCCESS);
            result.setMsg("确认成功！");
            MChangeOrder mChangeOrder = dao.getMChangeOrderByPrimaryKey(param.getCeoId());
            logService.saveLog("确认还料成功！","项目号：【"+mChangeOrder.getCeoNeedCode()+"】，跟踪号：【"+mChangeOrder.getCeoNeedTrackCode()+"】的还料已确认。");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setType(JsonResult.ResultType.ERROR);
            result.setMsg("确认失败！");
        }
        return result;
    }
    /**
     * ignoreTips 忽略此次还料提醒
     *
     * @param param 参数
     * @return com.zhongwang.mms.common.JsonResult
     * @throws
     * @author 徐一贺
     * @date 2020/11/28 12:41
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult ignoreTips(ChangeOrderRetMatModel param) {
        JsonResult result = new JsonResult();
        try {
            MChangeOrder mChangeOrder = dao.getMChangeOrderByPrimaryKey(param.getCeoId());
            dao.updateMOrderDetailByQuery(mChangeOrder);
            logService.saveLog("还料提醒","此次项目号：【"+mChangeOrder.getCeoNeedCode()+"】，跟踪号：【"+mChangeOrder.getCeoNeedTrackCode()+"】的还料提醒已忽略。");
            result.setType(JsonResult.ResultType.SUCCESS);
            result.setMsg("确认成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setType(JsonResult.ResultType.ERROR);
            result.setMsg("确认失败！");
        }
        return result;
    }
}
